package com.ad.pet.app.account.order.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.MainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.account.adapter.OrderTypeAdapter;
import com.ad.pet.common.data.http.BusCall;
import com.ad.pet.common.data.utils.ui.DialogUtil;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.LoadingFragment;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.account.order.BigOrder;
import com.ad.pet.models.remote.account.order.OrderType;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;

/**
 * 13620953972
 * Created by SEELE on 2019/2/15.
 */

public class OrderIndoorFragment extends LoadingFragment implements OrderStatusContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @Inject
    OrderStatusPresenter mStatusPresenter;

    private String mStatus;
    private OrderTypeAdapter mOrderTypeAdapter;

    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Inject
    RxDisposable mRxDisposable;
    private int page = 1;
//    private PayChoseDialog payChoseDialog;


    @Override
    protected int contentLayout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvents() {
        mRxDisposable.add(RxBus.getDefault().toObservable(String.class)
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new BusCall<String>() {
                    @Override
                    public void call(String webTitle) {
                        if ("confim_success".equals(webTitle)) {
                            page=1;
                            mStatusPresenter.myAppointList(mStatus, String.valueOf(page));
                        }
                    }
                }));

//        payChoseDialog = new PayChoseDialog(getActivity(), R.style.custom_dialog_style);

        mOrderTypeAdapter.setOnItemGoodClickLinstener(new OrderTypeAdapter.OnItemGoodClickLinstener() {
            @Override
            public void onCancel(final int position) {
                DialogUtil.createDialog(getActivity(), "", "是否取消预约?", true, "取消", null, "确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mStatusPresenter.cancelAppointList(mOrderTypeAdapter.getItem(position).getAppoint_id());
                    }
                }).show();
            }
        });



        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mOrderTypeAdapter.setEnableLoadMore(false);
                mStatusPresenter.myAppointList(mStatus, String.valueOf(page));

            }
        });

        mOrderTypeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mStatusPresenter.myAppointList( mStatus,String.valueOf(++page));
            }
        }, mRecyclerView);


    }

    @Override
    protected void initViews() {
        mStatusPresenter.onCreate(this);
        mStatus = getArguments().getString(Constant.STATUS);
//        mPacketInfoPresenter.onCreate(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mOrderTypeAdapter = new OrderTypeAdapter(null);
        mRecyclerView.addItemDecoration(new SimpleDividerDecoration(getActivity(), 9, R.color.bg_color));
        mRecyclerView.setAdapter(mOrderTypeAdapter);



        mStatusPresenter.myAppointList( mStatus,String.valueOf(page));

    }

    @Override
    public void initInjector() {
        getComponent(MainActivityCompent.class).inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    public static OrderIndoorFragment newInstance(String status) {
        Bundle args = new Bundle();
        OrderIndoorFragment fragment = new OrderIndoorFragment();
        args.putString(Constant.STATUS, status);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
//        mStatusLayoutManager.showEmpty();
    }

    @Override
    public void error(int code, String msg) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }

    }



    @Override
    public void getPendingSuccess(List<BigOrder> bigOrder) {

    }

    @Override
    public void getPacketInfoSuccess(BigOrder bigOrder) {

    }

    @Override
    public void getOrderStatusSuccess(List<BigOrder> orderStatuses) {

    }

    @Override
    public void cancelOrderSuccess() {

    }

    @Override
    public void confirmOrderSuccess() {

    }

    @Override
    public void getGoodOrderDetailSuccess(BigOrder bigOrder) {

    }

    @Override
    public void myAppointListSuccess(List<OrderType> orderStatuses) {
        mStatusLayoutManager.showContent();
        mSwipeRefreshLayout.setRefreshing(false);
        if (page == 1) {
            mOrderTypeAdapter.setNewData(orderStatuses);
        } else {
            mOrderTypeAdapter.addData(orderStatuses);
            mOrderTypeAdapter.loadMoreComplete();
        }
        if (orderStatuses.size() < 10) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            mOrderTypeAdapter.loadMoreEnd();
        }
        if (orderStatuses.size() == 0 && page == 1) {
            mOrderTypeAdapter.setEmptyView(R.layout.layout_empty_address);
            ((TextView)mOrderTypeAdapter.getEmptyView().findViewById(R.id.tv_title)).setText("您还没有相关服务哦");
//       您还没有相关服务哦
        }



    }

    @Override
    public void cancelAppointListSuccess() {
        RxBus.getDefault().post("confim_success");
        ToastUtil.getToastUtil().showShort("取消预约成功");
    }


}
